import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import 'package:flutter_form_builder/flutter_form_builder.dart';

import '../../util/app_util.dart';
import '../../blocs/cart.dart';

class OrderCommentPage extends StatefulWidget {
// class OrderTimePage extends StatelessWidget {
  const OrderCommentPage({Key? key}) : super(key: key);
  @override
  _OrderCommentPageState createState() => _OrderCommentPageState();
}

class _OrderCommentPageState extends State<OrderCommentPage> {
  @override
  initState() {
    super.initState();
  }

  GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    CartBloc _cart = Provider.of<CartBloc>(context, listen: false);

    ValueChanged _onChangedDate = (val) {};

    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text('备注', style: TextStyle(color: AppColor.blackMain)),
      ),
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              FormBuilder(
                key: _fbKey,
                autovalidateMode: AutovalidateMode.always,
                initialValue: {
                  "comment": _cart.ticket['comment'],
                },
                enabled: false,
                child: Column(
                  children: <Widget>[
                    Container(
                      // decoration: AppDecoration.buttonTwo, //
                      margin: EdgeInsets.symmetric(horizontal: 50.w),
                      padding: EdgeInsets.symmetric(
                        horizontal: 10.w,
                      ),
                      child: ButtonTheme(
                        alignedDropdown: true,
                        child: FormBuilderTextField(
                          name: "comment",
                          decoration: InputDecoration(
                            labelText: '备注', //,AppText.text['login_region_label'],
                          ),
                          //Text(AppText.text['login_region_tooltip']),
                          maxLines: 5,
                          minLines: 3,
                          // validators: [],
                          keyboardType: TextInputType.text,
                          onChanged: _onChangedDate,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(vertical: 16.0),
                child: FlatButton(
                  child: Text('确认'),
                  onPressed: () async {
                    _fbKey.currentState?.save();

                    if ((_fbKey.currentState?.value['comment'] ?? '').isNotEmpty) {
                      Map<String, dynamic> lm_param = {};
                      lm_param = {
                        'comment': _fbKey.currentState?.value['comment'],
                      };

                      var lv_result = await _cart.checkoutUpdate(lm_param);

                      if (lv_result)
                        Navigator.pop(context);
                      else
                        Navigator.pop(context);
                    } else {
                      print('sss');
                    }
                  },
                ),
              ),
              SizedBox(),
            ],
          ),
        ),
      ),
    );
  }
}
